package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustException;
import com.heima.feigns.AuthorFeign;
import com.heima.feigns.WemediaFeign;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.constants.admin.AdminConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDTO;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    ApUserMapper apUserMapper;
    @Autowired
    WemediaFeign wemediaFeign;
    @Autowired
    AuthorFeign authorFeign;


    /**
     * 根据状态查询需要认证相关的用户信息
     *
     * @param
     * @return
     */
    @Override
    public ResponseResult loadListByStatus(AuthDTO dto) {
        //1.校验业务参数
        if (dto == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "参数不能为空");
        }
        dto.checkParam();
        //2.条件查询
        Page<ApUserRealname> userPage = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();

        if (dto.getStatus() != null) {
            wrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        IPage<ApUserRealname> resultPage = this.page(userPage, wrapper);

        //返回结果

        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), resultPage.getTotal(), resultPage.getRecords());


        return result;
    }

    /**
     * 根据状态进行审核
     *
     * @param dto
     * @param status 2 审核失败   9 审核成功
     * @return
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public ResponseResult updateStatusById(AuthDTO dto, Short status) {
        //1.参数检查
        if (dto.getId() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询当前认证用户是否存在 APP端有当前用户
        ApUserRealname apUserRealname = this.getOne(Wrappers.<ApUserRealname>lambdaQuery()
                .eq(ApUserRealname::getId, dto.getId()));
        if (apUserRealname == null) {
            log.error("待审核 实名认证信息不存在   userRealnameId:{}", dto.getId());
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (!AdminConstants.WAIT_AUTH.equals(apUserRealname.getStatus())) {
            log.error("实名认证信息非待审核状态   userRealnameId:{}", dto.getId());
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW);
        }

        ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery()
                .eq(ApUser::getId, apUserRealname.getUserId()));

        if (apUser == null) {
            log.error("实名认证信息 关联 app的用户不存在    userRealnameId:{}, userId:{} ", dto.getId(), apUserRealname.getUserId());
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.更新认证用户信息
        apUserRealname.setStatus(status);
        apUserRealname.setUpdatedTime(new Date());
        if (StringUtils.isNotBlank(dto.getMsg())) {
            apUserRealname.setReason(dto.getMsg());
        }
        updateById(apUserRealname);


        if (AdminConstants.PASS_AUTH.equals(status)){
        // TODO 使用身份证OCR 扫描身份证 正面信息 得到姓名 和 身份证号

            // TODO 调用公安部接口,判断姓名身份证号是否真实


            //TODO 调用活体检测接口  判断照片是否和真实活人自拍照  得分大于85分算通过

            //TODO 调用认证检验接口  判断火兔检测图片 和身份证图片是否为一个人  得分大于等于85分算通过

        }




        //4.认证状态如果为通过
        if (AdminConstants.PASS_AUTH.equals(status)){
            //4.1创建自媒体账户
            WmUser wmUser = createWmUser(dto, apUser);
            //创建作业信息
            createApAuthor(wmUser);
        }
        return ResponseResult.okResult();
    }

    /**
     * 4.2 创建作者信息
     * @param wmUser
     * @return
     */
    private void createApAuthor(WmUser wmUser){
        //1.检查是否调用成功
        ResponseResult<ApAuthor> apAuthorResult = authorFeign.findByUserId(wmUser.getApUserId());
        if (apAuthorResult.getCode().intValue()!=0){
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR,apAuthorResult.getErrorMessage());
        }

        //2. 检查作者信息是否已经存在
        ApAuthor apAuthor = apAuthorResult.getData();
        if (apAuthor!=null){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"作者信息已存在");
        }

        //3.添加作者信息
        apAuthor = new ApAuthor();
        apAuthor.setCreatedTime(new Date());
        apAuthor.setName(wmUser.getName());
        apAuthor.setType(AdminConstants.AUTHOR_TYPE); // 自媒体人类型
        apAuthor.setUserId(wmUser.getApUserId()); // APP 用户ID
        apAuthor.setWmUserId(wmUser.getId()); // 自媒体用户ID
        ResponseResult result = authorFeign.save(apAuthor);
        //4. 结果失败，抛出异常
        if (result.getCode() != 0) {
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR,result.getErrorMessage());
        }
    }
    /**
     * 创建自媒体账户
     * @param dto
     * @param apUser  app端用户
      * @return
     */
    private WmUser createWmUser(AuthDTO dto,ApUser apUser){
        //1.查询自媒体账号是否存在(APP端用户密码和自媒体密码一致)
        ResponseResult<WmUser> wmUserResult = wemediaFeign.findByName(apUser.getName());
        if (wmUserResult.getCode().intValue()!=0){
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR,wmUserResult.getErrorMessage());
        }
        WmUser wmUser = wmUserResult.getData();
        if (wmUser!=null){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"自媒体用户信息已存在");
        }
        wmUser = new WmUser();
        wmUser.setName(apUser.getName());
        wmUser.setSalt(apUser.getSalt());  // 盐
        wmUser.setPassword(apUser.getPassword()); // 密码
        wmUser.setPhone(apUser.getPhone());
        wmUser.setCreatedTime(new Date());
        wmUser.setType(0); // 个人
        wmUser.setApUserId(apUser.getId());  // app端用户id
        wmUser.setStatus(AdminConstants.PASS_AUTH.intValue());

        ResponseResult<WmUser> save = wemediaFeign.save(wmUser);
        if (save.getCode().intValue()!=0){
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR,save.getErrorMessage());
        }

        return save.getData();
    }
}
